package com.jd.ty.web;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;
import java.util.List;


import com.jd.ty.service.FunctionService;
import com.jd.ty.service.UserService;
import com.jd.ty.service.exception.ErrorMessage;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.DES;
import com.jd.ty.util.Pager;

import com.jd.ty.pojo.Function;
import com.jd.ty.pojo.Permission;
import com.jd.ty.pojo.User;

public class UserAction extends BaseAction {

	private static final long serialVersionUID = -4018571604337249963L;

	private Long id;




	private UserService userService;

	private User user = new User();
	private List<Function> functions;
	private FunctionService functionService;
	private Long[] permission;



	@Override
	public String execute() throws Exception {

		return SUCCESS;
	}



	public String userInfo() {
		if (id != null) {
			user = userService.get(id);
		}
		return SUCCESS;
	}

	public String updateUser() throws ServiceException {
		try {
			if (user.getUserId() == null) {
				userService.add(user);
			} else {
				userService.update(user);
			}
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}
		return SUCCESS;
	}



	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Pager getPager() {
		return pager;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Function> getFunctions() {
		return functions;
	}

	public void setFunctions(List<Function> functions) {
		this.functions = functions;
	}

	public String changepwd() {
		User nuser = (User) userService.load(super.getUser().getUserId());
		nuser.setLoginPwd(DES.encrypt(user.getLoginPwd()));
		try {
			userService.update(nuser);
		} catch (ServiceException e) {
			log.error("change user password error.", e);
			addActionError(e.getErrorMessage());
			return ERROR;
		} catch (Exception e) {
			log.error("change user password error.", e);
			addActionError(ErrorMessage.UNKNOW_ERROR);
			return ERROR;
		}
		addActionMessage("密码已修改");
		return SUCCESS;
	}

	public String permissionInfo() {
		user = userService.load(id);
		functions = functionService.getTree();
		// functions=new ArrayList<Function>();
		// Function f=new Function();
		// f.setFunctionName("asdf");
		// f.setFunctionId(new Long(1));
		//				
		// Set<Function> children = new HashSet<Function>();
		// Function f1=new Function("ffff");
		// f1.setFunctionId(new Long(0));
		// children.add(f1);
		// f.setChildren(children);
		// functions.add(f);
		//				
		// Set<Permission> permissions = new HashSet<Permission>();
		// Permission o=new Permission();
		// o.setFunction(f1);
		// permissions.add(o);
		// user.setPermissions(permissions);
		return SUCCESS;
	}

	public String userMenu() {
		List<Function> funs = functionService.getTree();
		functions = new ArrayList<Function>();
		for (Function fun : funs) {
			functions.add(fun);
		}
		if(super. getUser().getRole().isAdmin()){
			return SUCCESS;
		}
		List<Permission> pers =super. getUser().getRole().getPermissions();
		Collections.sort(pers);
		for (Function function : functions) {
			List<Function> child = new ArrayList<Function>();
			for (Iterator<Permission> iterator = pers.iterator(); iterator.hasNext();) {
				Function pf = iterator.next().getFunction();
				if (pf.getParent().getFunctionId().longValue() == function.getFunctionId()
						.longValue())
					child.add(pf);
			}
			Collections.sort(child);
			function.setChildren(child);
		}
		// functions=functionService.getTree();
		return SUCCESS;
	}

	public String showMain() {
//		if(super.getUser().getRole().isAdmin()){
//			return SUCCESS;
//		}
//		List<Permission> pers = getUser().getRole().getPermissions();
//		for (Permission permission : pers) {
//			if (permission.getFunction().getIsDefault()) {
//				this.session.put(SessionKey.DEFAULT_FUNCTION, permission.getFunction());
//				return SUCCESS;
//			}
//		}
//		this.session.put(SessionKey.DEFAULT_FUNCTION, pers.get(0).getFunction());
		return SUCCESS;

	}

	public FunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public Long[] getPermission() {
		return permission;
	}

	public void setPermission(Long[] permission) {
		this.permission = permission;
	}


	public int getPage() {
		return page;
	}

	public void setPage(int page) {

		this.page = page;

	}

}
